<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Article extends Controller
{
	public function type(){
	  $type =Db::table('type')->select(); 
	  return $type;
	}
    public function index()
    {   
		//lanmu
	     
		// var_dump($list);
	   
		 $this->assign('type',$this->type());
		 //

		$where=array();
		$this->assign('title','');
		if(!empty(input('title'))){
		  $where['title'] = ['like','%'.input('title').'%'];
		  $this->assign('title',input('title'));
		}
		if(!empty(input('tid'))){
		  $where['type.tid'] = input('tid');
		  $this->assign('tid','');
		   $this->assign('tid',input('tid'));
		}
		 $list =Db::table('article')->join('type','type.tid=article.tid')->where($where)->paginate(10); 
		// var_dump($list);
	     $page = $list->render();
		 $this->assign('list',$list);
		 $this->assign('page',$page);
		 return $this->fetch(); 
    }

public function edit($aid)
    {  
		if(!empty($_POST['submit'])){
			
			 $re=Db::table('article')->update(['title' => input('title'),'aid'=>input('aid'),'tid'=>input('tid'),'content'=>input('content')]);
			  if($re){
			     $this->success('修改成功');
			  }else{
			    $this->errer('修改失败');
			}			   

		}else{
			$this->assign('type',$this->type());
            $rs=Db::table('article')->where('aid',$aid)->find();
		    $this->assign('rs',$rs);
			return view();
		}
    }
	public function add()
    {  
		if(!empty($_POST['submit'])){
			  $data = ['title' => $_POST['title'], 'tid' => $_POST['tid'],'addtime'=>time(),'content'=>$_POST['content']];
			  $re=Db::table('article')->insert($data);
			  if($re){
			     $this->success('新增成功');
			  }else{
			    $this->errer('新增失败');
			}			   

		}else{
			$this->assign('type',$this->type());
			return view();
		}
    }
	public function del($aid){
	   Db::table('article')->delete($aid);
	   $this->success('删除成功');
	}

	public function pdel(){
	   $aid=input('aid/a');
	   Db::table('article')->delete($aid);
	   $this->success('删除成功');
	}

	 
}
